﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi
{
    public partial class wfAgentDjackManage : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void DoAction()
        {
            if (this.strAction == "changeArea")
            {
                RefreshArea();
            }
            else if (this.strAction == "AddNewDjk")
            {
                AddNewDjack();
            }
        }

        /// <summary>
        /// 刷新地区信息
        /// </summary>
        private void RefreshArea()
        {
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");

            System.Data.DataSet ds1, ds2;

            using (msBsc.AppLogic.DataAlc.CBizCountryCity objCBizCountryCity = new AppLogic.DataAlc.CBizCountryCity())
            {
                ds1 = string.IsNullOrEmpty(countryId) ? null : objCBizCountryCity.SelectCountryByParentId(countryId);

                ds2 = string.IsNullOrEmpty(cityId) ? null : objCBizCountryCity.SelectCountryByParentId(cityId);
            }

            if (ds1 != null)
            {
                var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds1.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#country_city_list').html();
                            laytpl(_html).render(" + countryJson + @", function (render) {
                                $('#selCity').html(render);
                            });");
            }

            if (ds2 != null)
            {
                var cityJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds2.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#city_des_list').html();
                            laytpl(_html).render(" + cityJson + @", function (render) {
                                $('#selDestination').html(render);
                            });");
            }
        }

        /// <summary>
        /// 添加新地接客信息
        /// </summary>
        private void AddNewDjack()
        {
            string djkName = this.GetParamHtValue("djkName");
            if (string.IsNullOrWhiteSpace(djkName))
            {
                this.ExecJScript(@"ShowErrMsg(""txtDjkName"", ""地接客姓名不能为空或空格"");");
                return;
            }


            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");
            string destinationId = this.GetParamHtValue("destinationId");

            if (string.IsNullOrWhiteSpace(countryId)
                || string.IsNullOrWhiteSpace(cityId)
                || string.IsNullOrWhiteSpace(destinationId)
                || countryId == "0"
                || cityId == "0"
                || destinationId == "0")
            {
                this.ExecJScript(@"ShowErrMsg(""selDestination,#selCountry,#selCity"", ""地区信息不能为空"");");
                return;
            }

            string telPhone = this.GetParamHtValue("telPhone");
            if (string.IsNullOrWhiteSpace(telPhone))
            {
                this.ExecJScript(@"ShowErrMsg(""txtPhone"", ""地接客电话不能为空或空格"");");
                return;
            }

            string serviceArea = this.GetParamHtValue("serviceArea");
            if (string.IsNullOrWhiteSpace(serviceArea))
            {
                this.ExecJScript(@"ShowErrMsg(""txtServiceArea"", ""地接客服务区域不能为空或空格"");");
                return;
            }

            string tencentQQ = this.GetParamHtValue("tencentQQ");
            string weChatNo = this.GetParamHtValue("weChatNo");

            string agentId = this.GetParamHtValue("AgentId");
            if (string.IsNullOrWhiteSpace(agentId))
            {
                this.Alert("地接客所属代理不存在，请确认！");
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizCustomerRegister objCBizCustomerRegister = new CBizCustomerRegister())
            {
                DataSet ds = objCBizCustomerRegister.SelectById(agentId);

                if (ds == null
                    || ds.Tables[0].Rows.Count <= 0
                    || ds.Tables[0].Rows[0]["CUSTOMER_ACCOUNT_TYPE"].ToString() != "5")
                {
                    this.Alert("地接客所属代理不存在，请确认！");
                    return;
                }

                string[] addResult = objCBizCustomerRegister.AddAgentDjack(djkName, countryId, cityId, destinationId, telPhone, serviceArea, tencentQQ, weChatNo, agentId);

                if (addResult[0] == "FALSE")
                {
                    this.Alert(addResult[1]);
                    return;
                }

                this.Alert("添加成功！");
                return;
            }
        }
    }
}